package com.zhang.spark_2.com.zhang.core.wc

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @title:
 * @author: zhang
 * @date: 2022/2/17 09:47 
 */
object WC_countByValue {

  def main(args: Array[String]): Unit = {
    //TODO 获取环境
    val conf = new SparkConf().setMaster("local").setAppName("WordCount")
    val sc = new SparkContext(conf);

    val rdd = sc.makeRDD(
      List(("a", 1), ("b", 2), ("a", 1), ("b", 2), ("a", 1), ("b", 2)), 2
    )

    // todo 使用countByValue实现wordCount
    val word: RDD[String] = rdd.map {
      case (str, cnt) => {
        (str + " ") * cnt
      }
    }

    val wc: collection.Map[String, Long] = word.flatMap(_.split(" ")) countByValue()
    println(wc)

    sc.stop()
  }

}
